package com.jxs.android.weixi;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jxs.android.util.StartHelper;
import com.jxs.android.util.StatusHelper;
import com.jxs.android.util.StringUtils;
import com.jxs.android.weiget.LaucherTopBar;
import com.jxs.android.weiget.SpinnerButton;
import com.telecom.weixi.R;

public abstract class MyLaucher extends AbstractActivityGroup implements View.OnClickListener {

	public static final String SORT_ACTION = "com.jxs.android.sort_ACTION";

	public enum LaucherDirection {
		OPTION_1, OPTION_2, OPTION_3;

		public static LaucherDirection get(int order) {
			LaucherDirection direction = null;
			switch (order) {
			case 0:
				direction = OPTION_1;
				break;
			case 1:
				direction = OPTION_2;
				break;
			case 2:
				direction = OPTION_3;
				break;

			}
			return direction;
		}

	}

	protected FrameLayout bodyView;

	protected Context context;
	protected FrameLayout option_bar;
	protected LinearLayout option_1_ll, option_2_ll, option_3_ll;
	protected TextView option_1_tv, option_2_tv, option_3_tv;
	private String title;
	LaucherDirection direction = LaucherDirection.OPTION_1;
	protected int sortArrayid;
	LaucherTopBar topBar;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		context = this;
		if (null != getIntent() && null != getIntent().getExtras()) {
			title = getIntent().getExtras().getString("title");
		}
		initLaucherTopBar(savedInstanceState, R.layout.bu_laucher);
		bodyView = (FrameLayout) findViewById(R.id.list_bar);
		option_bar = (FrameLayout) findViewById(R.id.option_bar);
		initMainView();
		showView(direction, getSortArrayid());
	}

	protected void showView(LaucherDirection direction, int sortArrayid) {
		selected(direction, sortArrayid);
	}

	public abstract int getSortArrayid();

	protected void initMainView() {
		option_1_ll = (LinearLayout) findViewById(R.id.option_1_ll);
		option_2_ll = (LinearLayout) findViewById(R.id.option_2_ll);
		option_3_ll = (LinearLayout) findViewById(R.id.option_3_ll);

		option_1_tv = (TextView) findViewById(R.id.option_1_tv);
		option_2_tv = (TextView) findViewById(R.id.option_2_tv);
		option_3_tv = (TextView) findViewById(R.id.option_3_tv);

		option_1_ll.setOnClickListener(this);
		option_2_ll.setOnClickListener(this);
		option_3_ll.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == option_1_ll.getId()) {
			direction = LaucherDirection.OPTION_1;
		} else if (v.getId() == option_2_ll.getId()) {
			direction = LaucherDirection.OPTION_2;
		} else if (v.getId() == option_3_ll.getId()) {
			direction = LaucherDirection.OPTION_3;
		}
	}

	protected void selected(LaucherDirection direction, int sortArrayid) {
		topBar.setLogicBtnText(R.string.prompt_sort);
		if (direction.ordinal() == LaucherDirection.OPTION_1.ordinal()) {

			option_1_ll.setBackgroundResource(R.drawable.table_column);
			option_2_ll.setBackgroundResource(R.drawable.table_row0);
			option_3_ll.setBackgroundResource(R.drawable.table_row0);

			option_1_ll.setEnabled(false);
			option_2_ll.setEnabled(true);
			option_3_ll.setEnabled(true);

			option_1_tv.setSelected(true);
			option_2_tv.setSelected(false);
			option_3_tv.setSelected(false);

		} else if (direction.ordinal() == LaucherDirection.OPTION_2.ordinal()) {

			option_2_ll.setBackgroundResource(R.drawable.table_column);
			option_1_ll.setBackgroundResource(R.drawable.table_row0);
			option_3_ll.setBackgroundResource(R.drawable.table_row0);

			option_2_ll.setEnabled(false);
			option_1_ll.setEnabled(true);
			option_3_ll.setEnabled(true);

			option_2_tv.setSelected(true);
			option_1_tv.setSelected(false);
			option_3_tv.setSelected(false);

		} else if (direction.ordinal() == LaucherDirection.OPTION_3.ordinal()) {

			option_3_ll.setBackgroundResource(R.drawable.table_column);
			option_2_ll.setBackgroundResource(R.drawable.table_row0);
			option_1_ll.setBackgroundResource(R.drawable.table_row0);

			option_3_ll.setEnabled(false);
			option_2_ll.setEnabled(true);
			option_1_ll.setEnabled(true);

			option_3_tv.setSelected(true);
			option_2_tv.setSelected(false);
			option_1_tv.setSelected(false);

		}
		sorted(sortArrayid);
	}

	private void sorted(final int sortArrayid) {
		topBar.setResIdAndViewCreatedListener(R.layout.bu_laucher_top_bar_items, new SpinnerButton.ViewCreatedListener() {
			@Override
			public void onViewCreated(View v) {

				String[] items = getResources().getStringArray(sortArrayid);

				TextView sort1 = (TextView) v.findViewById(R.id.sort_item_1);
				TextView sort2 = (TextView) v.findViewById(R.id.sort_item_2);
				TextView sort3 = (TextView) v.findViewById(R.id.sort_item_3);
				TextView sort4 = (TextView) v.findViewById(R.id.sort_item_4);
				if (items.length > 3) {
					sort1.setText(items[0]);
					sort2.setText(items[1]);
					sort3.setText(items[2]);
					sort4.setText(items[3]);
				} else if (items.length == 3) {
					sort1.setText(items[0]);
					sort2.setText(items[1]);
					sort3.setText(items[2]);
					sort4.setVisibility(View.GONE);
				} else if (items.length == 2) {
					sort1.setText(items[0]);
					sort2.setText(items[1]);
					sort3.setVisibility(View.GONE);
					sort4.setVisibility(View.GONE);
				} else if (items.length == 1) {
					sort1.setText(items[0]);
					sort2.setVisibility(View.GONE);
					sort3.setVisibility(View.GONE);
					sort4.setVisibility(View.GONE);
				} else if (items.length == 0) {
					topBar.hindBtn();
				}

				View.OnClickListener clickListener = new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						topBar.dismisssLogicWind();
						topBar.setLogicBtnText(((TextView) v).getText().toString());
						sendConditionFilterBroadCast(v.getTag().toString());
					}
				};
				sort1.setOnClickListener(clickListener);
				sort2.setOnClickListener(clickListener);
				sort3.setOnClickListener(clickListener);
				sort4.setOnClickListener(clickListener);

			}

		});
	}

	protected void sendConditionFilterBroadCast(String sortkey) {
		Intent intent = new Intent(SORT_ACTION);
		Bundle bundle = new Bundle();
		bundle.putString("sortkey", sortkey);
		intent.putExtras(bundle);
		sendBroadcast(intent);
	}

	protected ProgressDialog progressDialog;

	public void initLaucherTopBar(Bundle savedInstanceState, int layoutId) {
		if (!this.getClass().getSimpleName().equals(Login.class.getSimpleName())) {
			if (null == WeixiApplication.getUserInfo() || StringUtils.isEmpety(WeixiApplication.getUserInfo().getTicket())) {
				errorMsgPop("登录已过期,请重新登录");
				StartHelper.startActivity(Login.class);
				finish();
			}
		}
		if (!StatusHelper.isNetworkAvailable(this)) {
			StatusHelper.checkNetworkAvailable(this);
		}
		super.onCreate(savedInstanceState);
		setContentView(layoutId);
		topBar = (LaucherTopBar) findViewById(R.id.topBar);
		if (null != topBar) {
			topBarLogic(topBar);
		}
	}

	public void topBarLogic(final LaucherTopBar topBar) {
		initLaucherTopBarReturn(topBar);
		topBar.setTitle(title);
		topBar.setLogicBtnText(R.string.prompt_sort);
	}

	public void initLaucherTopBarReturn(LaucherTopBar topBar) {
		if (null != topBar) {
			topBar.setTopBarReturnListern(new LaucherTopBar.ReturnBtnListern() {
				@Override
				public void onClickReturn() {
					finish();
				}
			});
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (!StatusHelper.isNetworkAvailable(this)) {
			StatusHelper.checkNetworkAvailable(this);
			return;
		}
	}

	public void show(LaucherDirection direction, Class<?> intentClass, int sortArrayid) {
		bodyView.removeAllViews();
		Intent intent = new Intent(this, intentClass);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
		bodyView.addView(getLocalActivityManager().startActivity(direction.ordinal() + "", intent).getDecorView());
		selected(direction, sortArrayid);
	}

}
